import axios from "axios";
import store from '../store'
import {Message} from 'element-ui';
let host = "";

axios.defaults.timeout = 50000;

//   axios.defaults.baseURL ='/'; //填写域名
 
//http request 拦截器

axios.interceptors.request.use(

    config => {
 
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

//响应拦截器即异常处理
axios.interceptors.response.use(response => {
     
    return response
}, err => {
    if (err && err.response) {
		
        switch (err.response.status) {
            case 400:
                console.log('错误请求')
                 Message.error('错误请求');
                break;
            case 401:
                console.log('未授权，请重新登录')
				 Message.error('未授权，请重新登录');
                break;
            case 403:
                console.log('拒绝访问')
				 Message.error('拒绝访问');
                break;
            case 404:
                console.log('请求错误,未找到该资源')
				 Message.error('请求错误,未找到该资源');
                break;
            case 405:
                console.log('请求方法未允许')
				 Message.error('请求方法未允许');
                break;
            case 408:
                console.log('请求超时')
				 Message.error('请求超时');
                break;
            case 500:
                console.log('服务器端出错')
				 Message.error('服务器端出错');
                
                break;
            case 501:
                console.log('网络未实现')
				 Message.error('网络未实现');
                break;
            case 502:
                console.log('网络错误')
				 Message.error('网络错误');
                break;
            case 503:
                console.log('服务不可用')
				 Message.error('服务不可用');
                break;
            case 504:
                console.log('网络超时')
				 Message.error('网络超时');
                break;
            case 505:
                console.log('http版本不支持该请求')
				 Message.error('http版本不支持该请求');
                break;
            default:
                console.log(`连接错误${err.response.status}`)
             Message.error(`连接错误${err.response.status}`);
        }
    } else {
         
        
    }
 
    return Promise.resolve(err.response)
})
// 登录后接口代token的写这里
let methodToken = [
     
    '/login',
    '/getcaptach',
    
     
]

/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */

export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
                params: params
            })
            .then(response => {
                resolve(response);
            })
            .catch(err => {
                reject(err)
            })
    })
}


/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url, data = {}) {
 

   /* if (methodToken.indexOf(url) >= 0) {
     alert(JSON.stringify(store))
        const {token=""} =store.state.token;
        if(token){
            data.token = token;
        }
        
    }*/
 
 
 //    if (methodToken.indexOf(url) == -1) {
 //        const {token=""} =store.state.user.userData;
 
 //        if(token){
     
 //            axios.defaults.headers.common["X-Token"] = token;
 //        }
        
 //    }else {

 //    }
 
    return new Promise((resolve, reject) => {
        axios.post(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
            })
    })
}
 
export function postcontent(url, data = {}) {
 
     return new Promise((resolve, reject) => {
         axios.post(url, data, {
            headers: {
              'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8"'
            },
          })
             .then(response => {
                 resolve(response);
             }, err => {
                 reject(err)
             })
     })
 }
export function postbold (url, data = {}) {
 

    /* if (methodToken.indexOf(url) >= 0) {
      alert(JSON.stringify(store))
         const {token=""} =store.state.token;
         if(token){
             data.token = token;
         }
         
     }*/
  
  
     if (methodToken.indexOf(url) == -1) {
         const {token=""} =store.state.user.userData;
  
         if(token){
      
             axios.defaults.headers.common["X-Token"] = token;
         }
         
     }else {
 
     }
     return new Promise((resolve, reject) => {
         axios.post(url, data,{responseType: 'blob'})
             .then(response => {
                 resolve(response);
             }, err => {
                 reject(err)
             })
     })
 }
 
/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function patch(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
            })
    })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.put(url, data)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err)
            })
    })
}
// 我的消息 /user/mymsglist
export const requestmymsglist= params => {
    
    return post('/user/mymsglist', params)
}; 
// 我的收藏/user/myconllect
export const requestmyconllect= params => {
    
    return post('/user/myconllect', params)
}; 
// 广播通知 /home/notice
export const requestnotice= params => {
    
    return post('/home/notice', params)
}; 
// /home/commentreplylist
export const requestcommentreplylist =params=>{
    return post('/home/commentreplylist', params)
}
// 喜欢/home/like
export const requestlike= params => {
    
    return post('/home/like', params)
}; 
// 收藏 /home/collect
export const requestcollect= params => {
    
    return post('/home/collect', params)
}; 
// 免密登录
export const requestFreePassLogin = params => {
    
    return post('/user/freelogin', params)
};

// 获取短信验证码

export const getSendcode = params => {
    
    return post('/user/sendcode', params)
};
// 密码登录
export const requestLogin = params => {
    
    return post('/user/pwdlogin', params)
};
// 微信检测验证码/home/checkwxcode
export const checkWxcode=params=>{
    return post('/home/checkwxcode', params)
}
//最近上新、热门标签、热度最高、收藏最多、猜你喜欢  /home/recommend
export const getRecomend=params=>{
    return post('/home/recommend', params)
}

// 获取首页/home/index
export const getHomeIndex = params => {
    
    return post('/home/index', params)
};
// 获取/home/content
export const getHomeContent = params => {
    
    return post('/home/content', params)
};
// 获取首页/home/page
export const getHomePage = params => {
    
    return post('/home/page', params)
};
// 评论/home/commentlist
export const getCommentList = params => {
    
    return post('/home/commentlist', params)
};
// 评论回复 /home/comment
export const getComment = params => {
    
    return post('/home/comment', params)
};
// 修改密码/user/uppwd
export const getUppwd = params => {
    
    return post('/user/uppwd', params)
};
// 修改个人信息/user/upprofile
export const getUpprofile = params => {
    
    return post('/user/upprofile', params)
};
// 回复我的/user/myreply
export const getMyreply = params => {
    
    return post('/user/myreply', params)
};
// 图片上传/user/upavatar
export const uploadPicture = params => {
    return post('/user/upavatar',
        params.formData,
        params.config
    );
};
// 搜索
export const getSearch = params => {
    
    return post('/home/search', params)
};
// 获取消息数量/user/mymsgnum
export const getMymsgnum = params => {
    
    return post('/user/mymsgnum', params)
};
 